﻿#include "algworker.h"
#include <string>
#include <QDebug>
AlgWorker::AlgWorker(QObject *parent) : QObject(parent)
{
	m_nResult = 0;
	memset(m_nNGorOK, 1, sizeof(int) * 100);
	memset(fIn_Round, 0, sizeof(float) * 100);
	memset(fOut_Round, 0, sizeof(float) * 100);
	memset(fIn_Len, 0, sizeof(float) * 100);
	memset(fOut_Len, 0, sizeof(float) * 100);
	memset(fLen, 0, sizeof(float) * 100);
}

AlgWorker::~AlgWorker()
{

}

void AlgWorker::Initialize()
{

}

static DWORD m_star, m_end;
static  int  m_count = 0;
void AlgWorker::start(CameraData m_cImageInfo)
{
	HalconCpp::HTuple w, h;
	HObject img;


	m_AlgCricle.para1 = m_cCam1;
	m_AlgSize.para2 = m_cCam2;
	CCD_34_PARAME ccCm3 = m_cCam3;
	CCD_34_PARAME ccCm4 = m_cCam4;


	m_nNum += 1;


	switch (m_cImageInfo.m_nIndex) {
	case 1:
		m_AlgCricle.m_place.x1 = m_algPlaceA.x1;
		m_AlgCricle.m_place.x2 = m_algPlaceA.x2;
		m_AlgCricle.m_place.y1 = m_algPlaceA.y1;
		m_AlgCricle.m_place.y2 = m_algPlaceA.y2;
		m_AlgCricle.m_place.hvCCDHandle = m_cImageInfo.hv_WindowHandle;

		m_nNGorOK[m_nResult] = m_AlgCricle.M_Cricle(m_cImageInfo.Hobj,
			true, In_Round, Out_Round, In_Len, Out_Len, img);

		//        fIn_Round[m_nResult] = In_Round.D();
		//        fOut_Round[m_nResult] = Out_Round.D();
		//        fIn_Len[m_nResult] = In_Len.D();
		//        fOut_Len[m_nResult] = Out_Len.D();
		show(img, m_cImageInfo.hv_WindowHandle, 1);
		break;
	case 2:
		m_AlgSize.m_placeA.x1 = m_algPlaceA.x1;
		m_AlgSize.m_placeA.x2 = m_algPlaceA.x2;
		m_AlgSize.m_placeA.y1 = m_algPlaceA.y1;
		m_AlgSize.m_placeA.y2 = m_algPlaceA.y2;
		m_AlgSize.m_placeA.hvCCDHandle = m_cImageInfo.hv_WindowHandle;


		m_AlgSize.m_placeB.x1 = m_algPlaceB.x1;
		m_AlgSize.m_placeB.x2 = m_algPlaceB.x2;
		m_AlgSize.m_placeB.y1 = m_algPlaceB.y1;
		m_AlgSize.m_placeB.y2 = m_algPlaceB.y2;
		m_AlgSize.m_placeB.hvCCDHandle = m_cImageInfo.hv_WindowHandle;



		m_nNGorOK[m_nResult] = m_AlgSize.M_Length(m_cImageInfo.Hobj, true, Len);
		//        fLen[m_nResult] = Len.D();
		show(m_cImageInfo.Hobj, m_cImageInfo.hv_WindowHandle, 2);
		break;
	case 3:

		m_AlgDefect.m_place.x1 = m_algPlaceA.x1;
		m_AlgDefect.m_place.x2 = m_algPlaceA.x2;
		m_AlgDefect.m_place.y1 = m_algPlaceA.y1;
		m_AlgDefect.m_place.y2 = m_algPlaceA.y2;
		m_AlgDefect.m_place.hvCCDHandle = m_cImageInfo.hv_WindowHandle;


		m_nNGorOK[m_nResult] = m_AlgDefect.Defect_detect(m_cImageInfo.Hobj, ccCm3, 3);

		show(m_AlgDefect.m_Image_Show, m_cImageInfo.hv_WindowHandle, 3);
		break;
	case 4:
		m_AlgDefect.m_place.x1 = m_algPlaceA.x1;
		m_AlgDefect.m_place.x2 = m_algPlaceA.x2;
		m_AlgDefect.m_place.y1 = m_algPlaceA.y1;
		m_AlgDefect.m_place.y2 = m_algPlaceA.y2;
		m_AlgDefect.m_place.hvCCDHandle = m_cImageInfo.hv_WindowHandle;



		m_nNGorOK[m_nResult] = m_AlgDefect.Defect_detect(m_cImageInfo.Hobj, ccCm4, 4);
		show(m_AlgDefect.m_Image_Show, m_cImageInfo.hv_WindowHandle, 4);
		break;
	default:
		break;
	}


	m_cImageInfo.m_nNGorOK = m_nNGorOK[m_nResult];
	if (nSave) {
		emit sigSaveImgStart(m_cImageInfo);
	}

	if (m_nModel) {
		return;
	}
	if (m_cImageInfo.m_nIndex == 4) {
		emit sigCameraResult(m_nResult);
	}
	m_nResult += 1;
	if (m_nResult == 100)
	{
		m_nResult = 0;
	}
}


void AlgWorker::show(HObject img, HTuple hv_WindowHandle, int index)
{
	TupleString(In_Len, ".4f", &In_Len);
	TupleString(Out_Len, ".4f", &Out_Len);
	TupleString(In_Round, ".4f", &In_Round);
	TupleString(Out_Round, ".4f", &Out_Round);
	TupleString(m_AlgCricle.Distance, ".4f", &Distance);
	TupleString(Len, ".4f", &Len);
	HTuple w, h;
	try
	{
		GetImageSize(img, &w, &h);
		SetPart(hv_WindowHandle, 0, 0, h, w);
		DispObj(img, hv_WindowHandle);
		switch (index) {
		case 1:

			SetTposition(hv_WindowHandle, HTuple(30), HTuple(1050));

			if (m_nNGorOK[m_nResult] != 0)
			{
				SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK[m_nResult] == 1) {
					WriteString(hv_WindowHandle, "NG");
				}
				else {
					WriteString(hv_WindowHandle, "异常");
				}
			}
			else
			{
				SetColor(hv_WindowHandle, "green");
				WriteString(hv_WindowHandle, "OK");
			}
			//内圆度
			if (m_AlgCricle.InRoundFlag)
			{
				SetColor(hv_WindowHandle, "green");
			}
			else
			{
				SetColor(hv_WindowHandle, "red");
			}
			SetTposition(hv_WindowHandle, HTuple(30), HTuple(50));
			WriteString(hv_WindowHandle, "内圆度=" + In_Round);
			//外圆度
			if (m_AlgCricle.OutRoundFlag)
			{
				SetColor(hv_WindowHandle, "green");
			}
			else
			{
				SetColor(hv_WindowHandle, "red");
			}
			SetTposition(hv_WindowHandle, HTuple(150), HTuple(50));
			WriteString(hv_WindowHandle, "外圆度=" + Out_Round);
			//内直径
			if (m_AlgCricle.InlenFlag)
			{
				SetColor(hv_WindowHandle, "green");
			}
			else
			{
				SetColor(hv_WindowHandle, "red");
			}
			SetTposition(hv_WindowHandle, HTuple(270), HTuple(50));
			WriteString(hv_WindowHandle, "内直径=" + In_Len);
			//外直径
			if (m_AlgCricle.OutlenFlag)
			{
				SetColor(hv_WindowHandle, "green");
			}
			else
			{
				SetColor(hv_WindowHandle, "red");
			}
			SetTposition(hv_WindowHandle, HTuple(390), HTuple(50));
			WriteString(hv_WindowHandle, "外直径=" + Out_Len);
			//圆心距
			if (m_AlgCricle.CricleFlag)
			{
				SetColor(hv_WindowHandle, "green");
			}
			else
			{
				SetColor(hv_WindowHandle, "red");
			}
			SetTposition(hv_WindowHandle, HTuple(510), HTuple(50));
			WriteString(hv_WindowHandle, "圆心距=" + Distance);
			break;
		case 2:
			SetTposition(hv_WindowHandle, HTuple(30), HTuple(2200));

			if (m_nNGorOK[m_nResult] != 0)
			{
				SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK[m_nResult] == 1) {
					WriteString(hv_WindowHandle, "NG");
				}
				else {
					WriteString(hv_WindowHandle, "异常");
				}
			}
			else
			{
				SetColor(hv_WindowHandle, "green");
				WriteString(hv_WindowHandle, "OK");
			}
			SetTposition(hv_WindowHandle, HTuple(30), HTuple(50));
			WriteString(hv_WindowHandle, "长度=" + Len);

			break;
		case 3:
			SetTposition(hv_WindowHandle, HTuple(30), HTuple(700));

			if (m_nNGorOK[m_nResult] != 0)
			{
				SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK[m_nResult] == 1) {
					WriteString(hv_WindowHandle, "NG");
				}
				else {
					WriteString(hv_WindowHandle, "异常");
				}

			}
			else
			{
				SetColor(hv_WindowHandle, "green");
				WriteString(hv_WindowHandle, "OK");
			}
			break;
		case 4:
			SetTposition(hv_WindowHandle, HTuple(30), HTuple(700));

			if (m_nNGorOK[m_nResult] != 0)
			{
				SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK[m_nResult] == 1) {
					WriteString(hv_WindowHandle, "NG");
				}
				else {
					WriteString(hv_WindowHandle, "异常");
				}
			}
			else
			{
				SetColor(hv_WindowHandle, "green");
				WriteString(hv_WindowHandle, "OK");
			}
			break;
		default:
			break;
		}

		HObject Rect, Contour;

		// 显示检测Roi区域
		HalconCpp::GenRectangle1(&Rect, m_algPlaceA.x1, m_algPlaceA.y1, m_algPlaceA.x2, m_algPlaceA.y2);
		HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
		HalconCpp::SetColor(hv_WindowHandle, "red");
		HalconCpp::DispObj(Contour, hv_WindowHandle);


		HalconCpp::GenRectangle1(&Rect, m_algPlaceB.x1, m_algPlaceB.y1, m_algPlaceB.x2, m_algPlaceB.y2);
		HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
		HalconCpp::SetColor(hv_WindowHandle, "red");
		HalconCpp::DispObj(Contour, hv_WindowHandle);

	}
	catch (HException &exception)
	{
	}
}
